package com.weedsdream.app.colorplate

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color

/**色盘颜色集<扇柄,扇面颜色集>.R-gb,G-rb,B-rg*/
internal val colorPointsR = arrayListOf<ColorPoint>()
internal val colorPointsG = arrayListOf<ColorPoint>()
internal val colorPointsB = arrayListOf<ColorPoint>()

/**色盘顶点位置集*/
internal val platePeakPoints = arrayListOf<Offset>()

/**颜色点
 * @property r 0-255
 * @property g 0-255
 * @property b 0-255
 * @property color 原色
 * @property show 实际显示的颜色
 * @property offset 三色轴极坐标转x,y坐标*/
internal data class ColorPoint(val r: Int, val g: Int, val b: Int) {
    val color = Color(r, g, b)
    var show = color
    var offset = color2offset(color)
}

/**初始化色点集
 * 可多次调用，但只有第一次生效
 * @param plateRadius 当前色盘半径*/
internal fun initColorPoints(plateRadius: Float) {
    //计算颜色迭代步长(影响颜色数)，计算每个颜色应当使用的半径
    var step = ((256.0 / plateRadius) - 0.0001).toInt() + 1//根据可用像素数计算可显示的颜色步长
    if (step < 64) step *= 5//255*255*3=195075(不含rgb轴)个像素点,全部渲染超过10s,降低色盘显示数(不影响触摸可获取的颜色值)。51(255/5)*51*3=7803(降低25倍)
    val count = 255 / step//根据步长计算每个轴实际可用的颜色点数(轴不含0值,所以不是256个点)
    pixelPerCP = plateRadius / count//计算颜色点尺寸
    //计算颜色点集
    if (colorPointsR.isEmpty()) for (g in 1 until 256 step step) {
        for (b in 1 until 256 step step) {
            colorPointsR.add(ColorPoint(0, g, b))
        }
    }
    if (colorPointsG.isEmpty()) for (b in 1 until 256 step step) {
        for (r in 1 until 256 step step) {
            colorPointsG.add(ColorPoint(r, 0, b))
        }
    }
    if (colorPointsB.isEmpty()) for (r in 1 until 256 step step) {
        for (g in 1 until 256 step step) {
            colorPointsB.add(ColorPoint(r, g, 0))//扇面点
        }
    }
    //计算色盘有效触摸范围及轴顶点坐标
    listOf(
        Color(255, 0, 0), Color(255, 255, 0), Color(0, 255, 0), Color(0, 255, 255),
        Color(0, 0, 255), Color(255, 0, 255)
    ).forEach { platePeakPoints.add(color2offset(it)) }
}